# NES_StateRecode.R


NES_StateRecode <- function (state, twoDigit = FALSE) {
  # Recode the state and country numbers in the ANES to the two-letter postal
  # abbreviations for states.  Ambiguous answers, refusals, and codes 
  # corresponding to other states are all counted as NA.  [2011 07 05]
  
  if (twoDigit) {
    state <- car::recode(
      state, 
      '01="CT"; 02="ME"; 03="MA"; 04="NH"; 05="RI"; 06="VT"; 
        11="DE"; 12="NJ"; 13="NY"; 14="PA"; 21="IL"; 22="IN"; 
        23="MI"; 24="OH"; 25="WI"; 31="IA"; 32="KS"; 33="MN"; 
        34="MO"; 35="NE"; 36="ND"; 37="SD"; 40="VA"; 41="AL";
        42="AR"; 43="FL"; 44="GA"; 45="LA"; 46="MS"; 47="NC"; 
        48="SC"; 49="TX"; 51="KY"; 52="MD"; 53="OK"; 54="TN"; 
        55="DC"; 56="WV"; 61="AZ"; 62="CO"; 63="ID"; 64="MT"; 
        65="NV"; 66="NM"; 67="UT"; 68="WY"; 71="CA"; 72="OR"; 
        73="WA"; else=NA', as.factor=TRUE) # 80="AK"; 81="HI";    
  }
  else {
    state <- car::recode(
      state, 
      '101="CT"; 102="ME"; 103="MA"; 104="NH"; 105="RI"; 106="VT"; 
        111="DE"; 112="NJ"; 113="NY"; 114="PA"; 121="IL"; 122="IN"; 
        123="MI"; 124="OH"; 125="WI"; 131="IA"; 132="KS"; 133="MN"; 
        134="MO"; 135="NE"; 136="ND"; 137="SD"; 140="VA"; 141="AL";
        142="AR"; 143="FL"; 144="GA"; 145="LA"; 146="MS"; 147="NC"; 
        148="SC"; 149="TX"; 151="KY"; 152="MD"; 153="OK"; 154="TN"; 
        155="DC"; 156="WV"; 161="AZ"; 162="CO"; 163="ID"; 164="MT"; 
        165="NV"; 166="NM"; 167="UT"; 168="WY"; 171="CA"; 172="OR"; 
        173="WA"; else=NA', as.factor=TRUE) # 180="AK"; 181="HI";
  }
}
